package com.nwpu.coursesupport.tool;

import com.nwpu.coursesupport.entity.Class.ClassDate;
import com.nwpu.coursesupport.enumeration.LessonStatus;
import com.nwpu.coursesupport.view.ClassView;

import java.util.Date;
import java.util.List;

import com.nwpu.coursesupport.entity.Class;
import com.nwpu.coursesupport.entity.Lesson;
import com.nwpu.coursesupport.entity.bean.Files;
import com.nwpu.coursesupport.entity.bean.Files.FileItem;

public class ServiceUtil {

	public static ClassDate parseDate(String date) {
		if (StringUtil.isEmpty(date)) {
			return null;
		}
		return DeserializeDate(date);
	}

	public static void parseLesson(List<Lesson> list) {

		list.forEach(item -> {
			Date now = new Date();
			if (item.getEndDate().before(now)) {// 结束时间小于现在
				item.setStatus(LessonStatus.FINISHED);
			} else if (item.getBeginDate().after(now)) {
				item.setStatus(LessonStatus.NOTSTART);
			} else if (item.getBeginDate().before(now) && item.getEndDate().after(now)) {
				item.setStatus(LessonStatus.INPROGRESS);
			}
		});
	}

	public static String serializeFiles(Files file) {
		if (file == null) {
			return null;
		}
		String result = JSONUtils.obj2json(file);
		return result;
	}

	public static Files DeserializeFiles(String s) {
		if (s == null) {
			return new Files();
		}
		Files file = JSONUtils.json2pojo(s, Files.class);
		return file;
	}
	
	public static Files DeserializeFilesForSafe(String s) {
		if (s == null) {
			return new Files();
		}
		Files file = JSONUtils.json2pojo(s, Files.class);
		for(FileItem item : file.getFiles()){
			item.setUrl(null);
		}
		return file;
	}

	public static String serializeDate(ClassDate date) {
		if (date == null) {
			return null;
		}
		/*
		 * RuntimeSchema<ClassDate> schema =
		 * RuntimeSchema.createFrom(ClassDate.class); byte[] bytes =
		 * ProtostuffIOUtil.toByteArray(date, schema,
		 * LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); return new
		 * String(bytes);
		 */
		String result = JSONUtils.obj2json(date);
		return result;
	}

	public static ClassDate DeserializeDate(String s) {
		if (s == null) {
			return null;
		}
		ClassDate date = JSONUtils.json2pojo(s, ClassDate.class);
		/*
		 * byte [] bytes=s.getBytes(); RuntimeSchema<ClassDate> schema =
		 * RuntimeSchema.createFrom(ClassDate.class); ClassDate date =
		 * schema.newMessage(); ProtostuffIOUtil.mergeFrom(bytes, date, schema);
		 */
		return date;
	}
}
